package com.yylending.las.lending.increase;

import android.text.TextUtils;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.common.AESEntity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Pengrf on 2017/11/21.
 * Copyright  2017 www.yyfax.com. All Rights Reserved.
 */
public class FinishPromoteProcessPresent extends Presenter<FinishPromoteProcessModel, FinishPromoteProcessPresent.View> {
	private View mView;

	public static class Generator implements PresenterFactory<FinishPromoteProcessPresent> {
		@Override
		public FinishPromoteProcessPresent create() {
			return new FinishPromoteProcessPresent();
		}
	}

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new FinishPromoteProcessModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public interface View extends IView {

		void finishPromoteProcessSuccess(FinishPromoteProcessModel.PromoteResult promoteResult);

		void finishPromoteProcessFail(String msg);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void finishPromoteProcessResult(FinishPromoteProcessModel.FinishPromoteResult result) {
		if (BaseResultEntity.isResponseSuccess(result)) {
			FinishPromoteProcessModel.PromoteResult promoteStatus = result.getData();

			if (promoteStatus != null) {
				mView.finishPromoteProcessSuccess(result.getData());
			} else {
				mView.finishPromoteProcessFail(TextUtils.isEmpty(result.getRetMsg()) ? "获取数据失败" : result.getRetMsg());
			}

		} else {
			mView.finishPromoteProcessFail(TextUtils.isEmpty(result.getRetMsg()) ? "获取数据失败" : result.getRetMsg());
		}
	}

	public void finishPromoteProcess(FinishPromoteProcessModel.FinishPromoteParams finishPromoteParams) {
		execute(new AESEntity(finishPromoteParams), FinishPromoteProcessModel.FINISH_PROMOTE_PROCESS, false, null, null);
	}
}
